from osv import osv, fields
import datetime
class kdvn_budget_history(osv.osv):
    _name='kdvn.budget.history'
    _description="KDVN Budget History"
    
    def create_history(self,cr,uid,prj_id):            
            #cr.execute(" % prj_id)
            material = 0.0
            sub = 0.0
            site = 0.0
            admin =0.0
            profit = 0.0
            list_history = {'date':datetime.datetime.now().strftime('%Y-%m-%d'),
                            'status':'draft',
                            'material':0.0,
                            'sub_contractor':0.0,
                            'site_expense':0.0,
                            'kinden_salary_admin_cost':0.0,
                            'bussiness_profit':0.0,                  
                            'amount':0.0,
                            'project_id':prj_id,
                            }
            cr.execute("Select \
                            cat_code,\
                            sum(coalesce(budget_amount,0)) as amount\
                        from \
                            kdvn_budget_data kbd\
                        left join\
                            kdvn_project kp on kbd.project_id=kp.id\
                        left join\
                            account_budget_post abp on kbd.budget_id = abp.id\
                        left join\
                            budget_category bc on abp.categ_id = bc.id\
                        where\
                            project_id in (%s) \
                        group by cat_code" %(prj_id))
            for cat_code,amount in cr.fetchall():
                list_history[cat_code] = amount
                list_history['amount']+= amount
            new_id = self.create(cr,uid,list_history)
            return new_id
    _order = "date desc"
    _columns={
         'project_id':fields.many2one('kdvn.project','Project'),
         'status':fields.selection([('draft','Draft'),('accepted','Accepted'),('rejected','Rejected')],'Status'),     
         'date':fields.date('Date'),
         'material':fields.float('Material'),
         'sub_contractor':fields.float('Sub-Contractor'),
         'site_expense':fields.float('Site-Expense'),
         'kinden_salary_admin_cost':fields.float("Salary, Admin. Cost"),
         'bussiness_profit':fields.float('Business Profit'),                  
         'amount':fields.float('Total')
         }
    _defaults={
            'status':lambda *x:'draft'
               }
kdvn_budget_history()
